//ajax
$("#tbl_user").bootstrapTable({
    url:"duzhe/all",
    method:'post',
    toolbar:'#toolbar',
    contentType:'application/x-www-form-urlencoded',
    sidePagination:'server',
    pageNumber:1,
    pageSize:5,
    queryParams:function(params){
        params["dz_name"] = $("#username").val();
        params["dz_shenFen.sf_id"] = $("#shenfen").val();
        return params;
    },
    pageList:[5, 25, 50, 100,'ALL'],
    dataType:'json',
    cache:false,
    pagination:true,

    showRefresh:true,
    showColumns:true,
    classes:'table table-hover table-striped',
    columns:[
        {
            checkbox:true
        },
        {
            visible:false,
            field:"dz_id",
            title:"德玛",
            align:"center"
        },{
            title:"序号",
            align:"center",
            formatter:function(value,row,index){
                return (index + 1);
            }
        },{
            field:"dz_name",
            title:"用户名",
            align:"center"
        },{
            field:"dz_shengfen",
            title:"身份证号",
            align:"center"
        },{
                field:"dz_shouji",
                title:"联系方式",
                align:"center"
        },{
                 field:"dz_dizhi",
                title:"家庭住址",
                align:"center"
        },{
            field:"dz_xinyu",
            title:"信誉分",
            align:"center"
        },{
            field:"dz_jine",
            title:"余额",
            align:"center"
        },{
            field:"dz_shenFen.sf_name",
            title:"用户等级",
            align:"center"
        },{
            title:"操作",
            align:"center",
            formatter:function (value, row, index) {
                var html = [

                    "<a href='javascript:void(0);' onclick='chongzhi(\"充值\",\"duzhe/chongzhi?id="+row.dz_id+"\")'><span class='glyphicon glyphicon-jpy'></span></a>&nbsp;&nbsp;&nbsp;&nbsp;",
                    "<a href='javascript:void(0);' onclick='toAdd(\"修改\",\"duzhe/update?id="+row.dz_id+"\")'><span class='glyphicon glyphicon-pencil'></span></a>&nbsp;&nbsp;&nbsp;&nbsp;",
                    "<a href='javascript:void(0);' onclick='delRow("+row.dz_id+")'><span class='glyphicon glyphicon-remove'></span></a>",
                ];
                return html.join("");
            }
        }
    ]

});

//删除一行数据
function delRow(id) {
    swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        cancelButtonText: "我再想想吧!",
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "删除",
        closeOnConfirm: false
    }, function () {
        $.ajax({
            url:'duzhe/delete',
            method:"post",
            data:{"id":id},
            success:function (data) {
                    swal("删除成功！", "您已经永久删除了这条信息。", "success");
                    //刷新表格
                    $("#tbl_user").bootstrapTable('refresh');
            }
        });


    });
}

//显示模态框并打开新增界面
function toAdd(title,url) {
    $("#modal1").find(".modal-title").html(title);
    $("#modal1").find(".modal-body").load(url);
    $("#modal1").modal('show');
}

function chongzhi(title,url) {
    $("#modal2").find(".modal-title").html(title);
    $("#modal2").find(".modal-body").load(url);
    $("#modal2").modal('show');
}


function jine(i) {
    var zhi=$(i).val();
    var dengji=$(i).parents(".form-group").next().find("#dz_shenFen");
    if(zhi < 100){
        $(dengji).val(4);
        swal("提示", "押金不可低于100", "error");
    }else if(zhi >= 100 && zhi < 200){
        $(dengji).val(4);
    }else if(zhi >= 200 && zhi < 300){
        $(dengji).val(1);
    }else if(zhi >= 300 && zhi < 500){
        $(dengji).val(2);
    }else if(zhi >= 500){
        $(dengji).val(3);
    }
}

function jine2(i) {
    var zhi=$(i).val();
    var xinyu = $(i).parents(".form-group").next().find("#dz_xinyu").val();
    var dengji=$(i).parents("#myForm").find("#dz_shenFen");
    var zongxinyu = parseInt(xinyu) + parseInt(zhi);
    if(zhi < 0 || zhi == undefined || zhi == ""){
        $(dengji).val(4);
        swal("提示", "金额输入错误", "error");
    }else if(zongxinyu >= 100 && zongxinyu < 200){
        $(dengji).val(4);
    }else if(zongxinyu >= 200 && zongxinyu < 300){
        $(dengji).val(1);
    }else if(zongxinyu >= 300 && zongxinyu < 500){
        $(dengji).val(2);
    }else if(zongxinyu >= 500){
        $(dengji).val(3);
    }
}

function chong(btn){
    var $form = $(btn).parents("#modal2").find("#myForm");
    $($form).find("#dz_shenFen").removeAttr("disabled");
    $($form).find("#dz_xinyu").removeAttr("disabled");
    var action = $form.attr("action");

    var zongjine = parseInt($($form).find("#dz_jine").val()) + parseInt($($form).find("#jine").val());
    var id= $($form).find("#id").val();
    var shenfen = $($form).find("#dz_shenFen").val();
    var xinyu = parseInt($($form).find("#dz_xinyu").val()) + parseInt($($form).find("#jine").val());

    $.ajax({
        url:action,
        method:"post",
        data:{"dz_id":id,"dz_jine":zongjine,"dz_shenFen.sf_id":shenfen,"dz_xinyu":xinyu},
        dataType:'json',
        success:function (data) {
            if(data.result){
                swal("成功", "操作成功", "success");

                $("#modal2").modal('hide');
                // //刷新表格
                $("#tbl_user").bootstrapTable('refresh');

            }
        }
    });
}

//提交表单
function submitForm(btn) {
    var $form = $(btn).parents("#modal1").find("#myForm");
    $($form).find("#dz_shenFen").removeAttr("disabled");
    var action = $form.attr("action");
    var str = $form.serialize();
    // var gongneng=$(btn).parents("#modal1").find(".modal-title").text();
    $.ajax({
        url:action,
        method:"post",
        //data:str,
        //FormData(dom)
        data:str,
        dataType:'json',
        // processData: false,
        // contentType: false,
        success:function (data) {
            if(data.result){
                swal("成功", "操作成功", "success");
                //隐藏模态框
                $("#modal1").modal('hide');
                //刷新表格
                $("#tbl_user").bootstrapTable('refresh');

            }
        }
    });
}

//按条件查询
function setCondition() {
    $("#tbl_user").bootstrapTable('refresh');
}

//批量删除
function batchDel() {
    //1.判断是否有行被选中
    //getSelections
    //如果没有行被选中,返回[]
    var rows = $("#tbl_user").bootstrapTable("getSelections");
    if(rows.length == 0){
        swal("提示", "请选中要删除的行", "warning");
        return;
    }
    //2.1获取要删除的行(主键)  []
    var model = {};
    for(var i = 0;i<rows.length;i++){
        model["ids["+i+"]"] = rows[i].dz_id
    }
//2.2传递数据到Controller
    swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        cancelButtonText: "我再想想吧!",
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "删除",
        closeOnConfirm: false
    }, function () {
        //ajax
        $.post('duzhe/batchDel',model,function (data) {
            if(data.result){
                swal("删除成功！", "您已经永久删除了这条信息。", "success");
                //刷新表格
                $("#tbl_user").bootstrapTable('refresh');
            }
        },'json');

    });
}

function batchAdd(title,url) {
    $("#modalExcel").find(".modal-title").html(title);
    $("#modalExcel").find(".modal-body").load(url);
    $("#modalExcel").modal('show');
}

function batchInsert() {
    var $form = $("#modalExcel").find('#myFormXls');
    var action = $form.attr('action');
    $.ajax({
        url:action,
        method:"post",
        data:new FormData($form.get(0)),
        dataType:'json',
        processData: false,
        contentType: false,
        success:function (data) {
            if(data.result){
                swal("添加成功！", "您已经永久添加了这些信息。", "success");
                //隐藏模态框
                $("#modalExcel").modal('hide');
                //刷新表格
                $("#tbl_user").bootstrapTable('refresh');

            }
        }
    });
}

function exportExcel(url) {
    var form = document.createElement('form');
    form.setAttribute("action",url);
    form.setAttribute("method","post");
    document.getElementsByTagName('body')[0].appendChild(form);
    form.submit();
}